package pinboard.dataLayer.binPack;

import java.util.HashMap;

import pinboard.dataLayer.CommHelper;

class BinSerializerCache {

    static HashMap<String, IBinSerializer> cache = new HashMap<>();

    public static IBinSerializer get(String className) {
        if (!cache.containsKey(className)) {
            try {
                Class<?> tClass = Class.forName(className);
                Object o = tClass.newInstance();
                IBinSerializer bs = CommHelper.as(o, IBinSerializer.class);
                if (bs != null) {
                    cache.put(className, bs);
                } else {
                    return null;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            } catch (InstantiationException e) {
                e.printStackTrace();
                return null;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return null;
            }
        }
        return cache.get(className);
    }
}